import java.util.ArrayList;

/**
 * Created by sunchenglin on 2017/7/1.
 输入一个链表，从尾到头打印链表每个节点的值。

 */
public class Solution {

    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        int temp,size;
        while(listNode!=null){
            list.add(listNode.val);
            listNode = listNode.next;
        }
        size = list.size();
        for (int i =0; i<size/2; i++){
            temp = list.get(i);
            list.set(i,list.get(size-1-i));
            list.set(size-1-i,temp);
        }
        return list;
    }

    public static void main(String args[]){

        Solution s = new Solution();
        ListNode node = new ListNode(-1);
        ListNode first = node;
        for(int i=0; i<5; i++){
            node.next = new ListNode(i);
            node = node.next;
        }

        System.out.println(s.printListFromTailToHead(first));
    }
}

class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}


